package com.jr.dao.impl;

import com.jr.dao.IWorkOrderDao;
import com.jr.pojo.WorkOrder;
import com.jr.util.DBHelper;
import com.jr.util.PageHelper;
import com.jr.vo.WorkOrderProject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class WorkOrderDaoImpl implements IWorkOrderDao {

    private DBHelper db=new DBHelper();

    @Override
    public int insertWorkOrder(WorkOrder wo) {
        String sql="insert into workorder values(null,?,?,?,?,?)";
        Object[] objs={wo.getPid(),wo.getExecutor(),wo.getDescription(),wo.getOrderlevel(),wo.getCreateDate()};
        return db.dtUpdate(sql,objs);
    }
//查询总条数
    @Override
    public int selectPageDateCount() {
        StringBuilder sql=new StringBuilder("select count(id) from workorder ");

        Connection con=null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        try {
            con = db.getCon();
            ps = con.prepareStatement(sql.toString());
            resultSet = ps.executeQuery();
            if (resultSet.next()) {
                return resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            db.closeAll(con,ps,resultSet);
        }
        return 0;
    }

//    查询当前页的数据
    @Override
    public ArrayList<WorkOrderProject> selectIndexDate(PageHelper ph) {

        String sql="select w.id,p.pname,w.EXECUTOR,w.DESCRIPTION,w.ORDERLEVEL,w.CREATEDATE" +
                " from workorder w inner join project p on w.pid=p.pid limit ?,?";

        int index=(ph.getPageIndex()-1)*ph.getShowNumber();

        return db.dtQuery(sql,WorkOrderProject.class,index,ph.getShowNumber());
    }
}
